package vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.patches;

import vg.services.data_base_manager.data.graph.Graph;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.SmartGraphView;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVGraphView4Patching;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.interfaces.ISGVPatch;
import vg.services.graph_view_manager.realization.graph_view.smart_graph_view.realization.SGVGraphView4Patching;
import vg.services.progress_manager.interfaces.IProgressTask;

/**
 * This patch merges two graph views.
 * @author Timur Zolotuhin (e-mail: tzolotuhin@gmail.com)
 */
public class MergePatch extends DefaultPatch {
	// Main data
	private SmartGraphView src, dst;
	private SGVGraphView4Patching src_sgv_graph_view4patching, dst_sgv_graph_view4patching;
	private Graph src_graph;
	private boolean completed = false;
	
	// Mutex
	private final Object generalMutex = new Object();
	
	public MergePatch(SmartGraphView src, SmartGraphView dst, SGVGraphView4Patching src_graph_view4patching, SGVGraphView4Patching dst_graph_view4patching, Graph src_graph) {
		super("Merge patch");
		
		this.src = src;
		this.dst = dst;
		
		this.src_sgv_graph_view4patching = src_graph_view4patching;
		this.dst_sgv_graph_view4patching = dst_graph_view4patching;
		
		this.src_graph = src_graph;
	}
	
	@Override
	public void apply(final ISGVGraphView4Patching dst_graphView4patching) {
		src.addPatch(new DefaultPatch("Merge patch") {
			@Override
			public void apply(ISGVGraphView4Patching src_graphView4patching) {
				synchronized (generalMutex) {
					dst_sgv_graph_view4patching.merge(src_sgv_graph_view4patching, src_graph);
					completed = true;
				}
			}
		});
		
		while(true) {
			try {
				Thread.sleep(100);
			} catch (Exception ex) {}
			
			synchronized (generalMutex) {
				if (completed)
					break;
			}
		}
		
	}
}
